// pages/post7/post7.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    job: {},
    agree:false,
    show:false,
  },
  agree:function(e){
    this.setData({
      agree:!this.data.agree
    })
  },
  see: function (e) {
    this.setData({
      show: !this.data.show
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenerEventChannel().on('jobTransfer', job => {
      this.setData({job: job});
    });
    core.get({url:'xilujob.common/single_page',data:{tag:'job_agreement'},success:ret=>{
      this.setData({agreement: ret.data.page.content.replace(/\<img/g,'<img style="width:100%;height:auto;display:block;" ')});
    }})
  },

  bindInput: function (event) {
    let name = event.currentTarget.dataset.name,
      value = event.detail.value;
    this.setData({
      ['job.'+name]: value
    });
  },

  bindSave: function (event) {
    if(!this.data.agree) {
      wx.showToast({title: '请阅读并同意《免责声明》',icon:'none'});
      return;
    }

    let validator = new WeValidator({
      rules: {
        contact_person: {required:true},
        contact_phone: {required:true},
        contact_email: {email:true},
      },
      messages: {
        contact_person: {required:'请填写联系人'},
        contact_phone: {required:'请填写联系电话'},
        contact_email: {email:'请填写正确的邮箱'},
      }
    });
    if(!validator.checkData(this.data.job)) {
      return;
    }
    if(!this.data.job.id) {
      core.post({
        url: '/xilujob.job_manage/add',
        data: this._formatPostData(),
        success: (ret) => {
          let job = ret.data.job;
          app.eventBus.emit(app.Events.JobCreate, job);
          if(getCurrentPages()[0].route==='pages/post/post') {
            wx.redirectTo({url: '/pages/fabu_management/fabu_management'});
          }else {
            wx.navigateBack({delta: 8});
          }
        }
      });
    }else {
      core.post({
        url: '/xilujob.job_manage/edit/id/'+this.data.job.id,
        data: this._formatPostData(),
        success: (ret) => {
          let job = ret.data.job;
          app.eventBus.emit(app.Events.JobUpdate, job);
          wx.navigateBack({delta: 8});
        }
      });
    }
  },
  _formatPostData: function () {
    let job = {
      "type": this.data.job.type,
      "job_name": this.data.job.job_name,
      "num": this.data.job.num,
      "job_category_id": this.data.job.job_category_id,
      "salary_type": this.data.job.salary_type,
      "salary_min": this.data.job.salary_min,
      "salary_max": this.data.job.salary_max,
      "responsibility": this.data.job.responsibility,
      "environment_images": this.data.job.environment_images,
      "red_packet": this.data.job.red_packet,
      "red_packet_instruction": this.data.job.red_packet_instruction,
      "benefit_ids": this.data.job.benefit_ids,
      "education_level_id": this.data.job.education_level_id,
      "work_experience_id": this.data.job.work_experience_id,
      "province_id": this.data.job.province_id,
      "city_id": this.data.job.city_id,
      "district_id": this.data.job.district_id,
      'town_id': this.data.job.town_id,
      "contact_person": this.data.job.contact_person,
      "contact_phone": this.data.job.contact_phone,
      "contact_email": this.data.job.contact_email,
      "locations": [],
    };
    this.data.job.locations.forEach(item => {
      // job.locations.push({
      //   "id": item.id,
      //   "province_id": item.province_id,
      //   "city_id": item.city_id,
      //   "district_id": item.district_id,
      //   "address": item.address,
      // });
      job.locations.push(item)
    });
    return job;

  },
});
